import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const views = import.meta.glob('../views/**/*.vue')
const children = Object.keys(views).map(key => {
  let name = key
    .replace(/\.vue$/, '')
    .replace(/\.\.\/views\//, '')
    .replace(/\\/g, '/')

  if (name === 'home') {
    name = '/'
  } else {
    name = `/${name}`
  }

  return {
    path: name,
    name: name.replace(/\//g, '-').slice(1),
    component: views[key],
  }
})

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/layouts/default.vue'),
    children,
  },
  {
    path: '/:catchAll(.*)',
    redirect: '/',
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})
router.beforeEach((to, from, next) => {
  next()
})
export default router
